package com.yunhe.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.ShoppingCarInfo;
import com.yunhe.inter.ShoppingCarInfoInter;
import com.yunhe.util.SqlSessionFactoryUtil;


@WebServlet("/ShoppingCarInfoServlet")
public class ShoppingCarInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
    public ShoppingCarInfoServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				//获取commodityID（加入购物车的商品ID）
				//判断获取的ID是否为空,用于从首页面直接进入购物车中
				Integer commodityID;
				if(request.getParameter("commodityID")==null||"".equals(request.getParameter("commodityID"))){
					commodityID = 0;
				}else{
					
					commodityID = Integer.parseInt(request.getParameter("commodityID")) ;
				}
				
				ShoppingCarInfo sci = new ShoppingCarInfo();
				//建立数据库连接
				SqlSession sqlSession =SqlSessionFactoryUtil.openSqlSession();
				ShoppingCarInfoInter mapper = sqlSession.getMapper(ShoppingCarInfoInter.class);
				//判断添加的id是否存在
				ShoppingCarInfo sult = mapper.findShoppingisExist(commodityID);
				
				//加载对应商品ID购物车信息供更新调用
				
				ShoppingCarInfo shoppingCar =  mapper.findShoppingInfo(commodityID);
				/**
				 * 处理点击加入购物车的信息
				 */
				//处理从首页点击购物车的问题
				if(commodityID!=0){
					if(sult==null){
						//将添加到购物车的ID入库
						mapper.addShoppingInfo(commodityID,sci.getQuantityPurchased(),sci.getUserID());
						sqlSession.commit();
						
					}else{
						//更新对应ID的购买数量
						sci.setQuantityPurchased(shoppingCar.getQuantityPurchased()+1);
						mapper.updateShoppingInfo(commodityID,sci.getQuantityPurchased());
						sqlSession.commit();
					}
					
				}
				
				
				
					
				
				//查询购物车中的信息
				ArrayList<ShoppingCarInfo> list  =mapper.findShoppingInfos();
				
				/**
				 * 获得购物车中所有的商品ID
				 */
				StringBuilder sb = new StringBuilder();
				//判断购物车里面是否有数据，防止空指针异常
				if(list.size()>0){
					for (ShoppingCarInfo shoppingCarInfo : list) {
						String commodityIDString=shoppingCarInfo.getCommodityID()+",";
						sb.append(commodityIDString);
						
					}
					
				}
				
				//添加判断，防止空指针异常
				if(sb!=null && "".equals(sb)){
					shoppingCar.setCommodityIDS(sb.toString());
					
				}
					
				//将list集合放入request中
				request.setAttribute("list", list);
				request.setAttribute("sb", sb);
				request.getRequestDispatcher("/shopping.jsp").forward(request, response);
			}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
	
	
	


}
